package lifecounter;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;

public class MainScreen extends Canvas{
    private int my_life;
    private int width;
    private int height;
    
    public MainScreen() {
        setFullScreenMode(true);
        this.my_life = 20;
    }

    protected void paint(Graphics g) {
        this.width = getWidth();
        this.height = getHeight();
        //set background color
        g.setColor(0x004b8b);
        g.fillRect(0, 0, width, height);
        //draw gain-loss button
        g.setColor(0x3b5998);
        g.fillRect(10, height-70, 50, 50);
        g.fillRect(width-60, height-70, 50, 50);
        //draw +1
        g.setColor(0xffffff);
        g.fillRect((width-60)+10,(height-70)+25,15,5);
        g.fillRect((width-60)+15,(height-70)+20,5,15);
        g.fillRect((width-60)+35,(height-70)+10,5,30);
        g.fillRect((width-60)+30,(height-70)+15,5,5);
        //draw -1
        g.fillRect(10+10,(height-70)+25,10,5);
        g.fillRect(10+35,(height-70)+10,5,30);
        g.fillRect(10+30,(height-70)+15,5,5);
        
        int center = width/2;
        String digit = Integer.toString(this.my_life);
        if(this.my_life>9) {
            for(int i=0;i<digit.length();i++) {
                int c = Integer.parseInt(digit.substring(i,i+1));
                if(i==0) {
                    this.drawNumber(g, c, center-45, height/3);
                }
                else {
                    this.drawNumber(g, c, center+5, height/3);
                }
            }
        } else {
            if(this.my_life <= 0) {
                this.drawNumber(g, 0, center-20, height/3);
            } else {
                this.drawNumber(g, this.my_life, center-20, height/3);
            }
        }
    }
    
    public void drawNumber(Graphics g, int num, int xpos, int ypos) {
        int x = num;
        g.setColor(0xffffff);
        if(x == 0) {
            g.fillRect(xpos+10, ypos+10, 20, 10);       //top
            g.fillRect(xpos, ypos+10, 10, 70);          //left v
            g.fillRect(xpos+30, ypos+10, 10, 70);       //right v
            g.fillRect(xpos+10, ypos+70, 20, 10);       //bottom 
        } else if(x == 1) {
            g.fillRect(xpos+10, ypos+20, 10, 10);       //top
            g.fillRect(xpos+20, ypos+10, 10, 70);       //left v
        } else if(x == 2) {
            g.fillRect(xpos, ypos+10, 40, 10);          //top
            g.fillRect(xpos+30, ypos+20, 10, 20);       //right v
            g.fillRect(xpos, ypos+40, 40, 10);          //mid
            g.fillRect(xpos, ypos+50, 10, 20);          //left v
            g.fillRect(xpos, ypos+70, 40, 10);          //bottom
        } else if(x == 3) {
            g.fillRect(xpos, ypos+10, 40, 10);          //top
            g.fillRect(xpos+30, ypos+10, 10, 70);       //right v
            g.fillRect(xpos+5, ypos+40, 35, 10);        //mid
            g.fillRect(xpos, ypos+70, 30, 10);          //bottom
        } else if(x == 4) {
            g.fillRect(xpos, ypos+10, 10, 30);          //left v
            g.fillRect(xpos, ypos+40, 30, 10);          //mid
            g.fillRect(xpos+30, ypos+10, 10, 70);       //right v
        } else if(x == 5) {
            g.fillRect(xpos, ypos+10, 40, 10);          //top
            g.fillRect(xpos, ypos+20, 10, 20);          //left v
            g.fillRect(xpos, ypos+40, 40, 10);          //mid
            g.fillRect(xpos+30, ypos+50, 10, 20);       //right v
            g.fillRect(xpos, ypos+70, 40, 10);          //bottom
        } else if(x == 6) {
            g.fillRect(xpos, ypos+10, 10, 70);          //left v
            g.fillRect(xpos+10, ypos+10, 30, 10);       //top
            g.fillRect(xpos+10, ypos+40, 30, 10);       //mid
            g.fillRect(xpos+30, ypos+50, 10, 20);       //right v
            g.fillRect(xpos+10, ypos+70, 30, 10);       //bottom
        } else if(x == 7) {
            g.fillRect(xpos, ypos+10, 40, 10);          //top
            g.fillRect(xpos+30, ypos+20, 10, 60);       //right v
        } else if(x == 8) {
            g.fillRect(xpos+10, ypos+10, 20, 10);       //top
            g.fillRect(xpos, ypos+10, 10, 70);          //left v
            g.fillRect(xpos+30, ypos+10, 10, 70);       //right v
            g.fillRect(xpos+10, ypos+70, 20, 10);       //bottom
            g.fillRect(xpos+10, ypos+40, 20, 10);       //mid
        } else if(x == 9) {
            g.fillRect(xpos+30, ypos+10, 10, 70);       //left v
            g.fillRect(xpos, ypos+10, 30, 10);          //top
            g.fillRect(xpos, ypos+40, 30, 10);          //mid
            g.fillRect(xpos, ypos+20, 10, 20);          //right v
            g.fillRect(xpos, ypos+70, 30, 10);          //bottom
        }
    }
    
    protected void pointerPressed(int x, int y) {
        //why i use 10+50 instead of 60 and etc? i dont know =P
        if(x>=10 && x<=10+50 && y>=(height-70) && y<=(height-70)+50) {
            this.my_life--;
            if(this.my_life < 0) {
                this.my_life=0;
            }
        } else if(x>=width-60 && x<=(width-60)+50 && y>=(height-70) && y<=(height-70)+50) {
            this.my_life++;
            if(this.my_life > 99) {
                this.my_life=99;
            }
        }
        repaint();
    }
    
}
